<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepositoryEloquent;
use App\User;
use App\Models\UserRelationship;
use Breadcrumbs, Hash,Auth,Toastr,Config,Validator,DB,Carbon;
use Illuminate\Http\Request;
use App\Http\Requests\Home\User\UpdatePasswordRequest;
//use App\Models\FinancialDetails;
use App\Models\Commission;

use EasyWeChat\Payment\Order;

class CommissionController extends Controller{

    protected $user;

    public function __construct(UserRepositoryEloquent $user)
    {
        //parent::__construct();
        $this->user = $user;


    }

    public function index(Request $request){
        $user = $request->user();
        //已分配
        $yescommissions = Commission::where('commission_user_id','=',$user->id)->where('status','=',1)->sum('commission');
        //$yescollection = collect($yescommissions);
        //$yescommissions = $yescollection->sum('commission');
        //未分配分配
        $nocommissions = Commission::where('commission_user_id','=',$user->id)->where('status','=',0)->sum('commission');
        //$nocollection = collect($nocommissions);
        //$nocommissions = $nocollection->sum('commission');

        //$subordinates = UserRelationship::where('superiorid','=',$user->id)->where('level','=',1)->orderBy('id','desc')->count();
        $subordinates = null;
        $userparentid=base64_encode($user->id);
    
        $share_url = url('/register/'.$userparentid);

        return view('home.commission.index',compact('user','share_url','yescommissions','nocommissions','subordinates'));
    }

    /**
     * [subordinates 我的经纪人]
     * @return [type] [description]
     */
    public function subordinates(Request $request){
        $user = $request->user();
        //$subordinates = UserRelationship::where('superiorid','=',$user->id)->where('level','=',1)->orderBy('id','desc')->simplePaginate(5);
        //$subordinates = $user->descendantsAndSelf()->withoutNode($user)->get()->toArray();
        $subordinates = $user->children()->simplePaginate(5);
        //dd($subordinates);
        return view('home.commission.subordinates',compact('subordinates'));
    }

    /**
     * [details 佣金明细]
     * @return [type] [description]
     */
    public function details(Request $request){
        $user = $request->user();
        $commissions = Commission::where('commission_user_id','=',$user->id)->simplePaginate(5);
        return view('home.commission.details',compact('commissions'));
    }
}